<?php
namespace app\api\model;

use app\admin\event\SystemLog;
use think\Model;
use think\Db;
use think\Exception;
use think\exception\DbException;
class FinanceModel extends Base
{

    protected static function init()
    {
        self::observe(SystemLog::class);
    }

    public function getContrastList($data,$field="",$order='id desc')
     {
         $where =[];
         if($data['min'] && $data['max']){
             $where[] =['arrears','between',[$data['min'],$data['max']]];
         }
         if(!empty($data['kid'])){
             $where[] =['uid','=',$data['kid']];
         }
         if(!empty($data['category'])){
             $where[] =['category','=',$data['category']];
         }
         if($data['state'] ==1){
             $where[] =['arrears','<',0];
         }
         $list['list'] =Db::name('user')->alias('u')
             ->leftJoin('customer_category c','c.id =u.category')
             ->field('u.id,u.name,category,contacts,phone,arrears,c.name as cateName')
             ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
         $arrears =[];
         foreach($list['list'] as $k=>$v){
             if($v['arrears'] < 0){
                 $arrears[] =$v['arrears'];
             }
         }
         $list['total'] =count($list['list']);
         $list['arrearsNum'] =count($arrears);
         $list['arrears'] =array_sum($arrears);
         return $list;
     }

     public function getAllAcount()
     {
         return $this->getselectList('account_type','id,name',['status'=>0]);
     }

    public function getSetAcount()
    {
        return $this->getselectList('set_account','id,name, money',['status'=>0]);
    }

//    public function getCustomerList($data,$field="",$order='id desc')
//    {
//        $where =[];
//        if(!empty($data['kid'])){
//            $where[] =['kid','=',$data['kid']];
//        }
//        if($data['start_time'] && $data['end_time']){
//            $where[] =['create_time','between',[$data['start_time'],$data['end_time']]];
//        }
//        if(!empty($data['type'])){
//            $where[] =['type','=',$data['type']];
//        }
//        if(!empty($data['set_account'])){
//            $where[] =['set_account','=',$data['set_account']];
//        }
//        if(!empty($data['maker'])){
//            $where[] =['maker','=',$data['maker']];
//        }
//        if(!empty($data['number'])){
//            $where[] =['number','like','%'.$data['number'].'%'];
//        }
//        $list =$this->getPage('sale','id,create_time,number,maker,set_account,type,total,payment,des',$where,'id desc',$data['page'],$data['size']);
//        $list =$list->toArray();
//        $total =[];
//        $payment =[];
//        $balance =[];
//        foreach ($list['data'] as $k=>$v){
//            $list['data'][$k]['total_balance'] = round(($v['total'] - $v['payment']), 2);
//            $list['data'][$k]['maker_name'] = Db::name('admin')->find($list['data'][$k]['maker'])['name'];
//            $list['data'][$k]['set_account_name'] = Db::name('set_account')->find($list['data'][$k]['set_account'])['name'];
//            $total[] =$v['total'];
//            $payment[] =$v['payment'];
//            $balance[] = round(($v['total'] - $v['payment']), 2);
//        }
//        $list['totals'] =array_sum($total);
//        $list['payment'] =array_sum($payment);
//        $list['balance'] =array_sum($balance);
//        return $list;
//    }
    public function getCustomerInfo($id)
    {
        $list=$this->getOne('sale','*',['id'=>$id]);
        return $list;
    }

    public function getSupplierList($data,$field="",$order='id desc')
    {
        $where =[];
        if(!empty($data['supplier'])){
            $where[] =['supplier','=',$data['supplier']];
        }
        if($data['start_time'] && $data['end_time']){
            $where[] =['create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if(!empty($data['type'])){
            $where[] =['type','=',$data['type']];
        }
        if(!empty($data['set_account'])){
            $where[] =['set_account','=',$data['set_account']];
        }
        if(!empty($data['maker'])){
            $where[] =['uid','=',$data['maker']];
        }
        if(!empty($data['number'])){
            $where[] =['number','like','%'.$data['number'].'%'];
        }
        $list =$this->getPage('purchase','id,create_time,number,uid,set_account,type,total,payment,des',$where,'id desc',$data['page'],$data['size']);
        $list =$list->toArray();
        $total =[];
        $payment =[];
        $balance =[];
        foreach ($list['data'] as $k=>$v){
            $list['data'][$k]['total_balance'] = $v['total'] - $v['payment'];
            $total[] =$v['total'];
            $payment[] =$v['payment'];
            $balance[] =$v['total'] - $v['payment'];
        }
        $list['totals'] =array_sum($total);
        $list['payment'] =array_sum($payment);
        $list['balance'] =array_sum($balance);
        return $list;
    }

    public function getSupplierInfo($id)
    {
        $list=$this->getOne('purchase','*',['id'=>$id]);
        return $list;
    }

     public function getAccountList()
     {
         $list =$this->getselectList('set_account','*',[],'sort asc');
         return $list;
     }

    public function getAccountAdd($data)
    {
        $list =$this->getAdd('set_account',$data);
        return $list;
    }

    public function getAccountInfo($id)
    {
        $list =$this->getOne('set_account','*',['id'=>$id]);
        $list['type_name'] = AccountType::find($list['type'])['name'];
        $list['store_name'] = Store::find($list['store'])['name'];
        return $list;
    }

    public function getAccountEdit($id,$data)
    {
        $list =$this->getEdit('set_account',['id'=>$id],$data);
        return $list;
    }

    public function getAccountDel($id)
    {
        $list =$this->getDel('set_account',$id);
        return $list;
    }

    public function getAccountType()
    {
        $list =$this->getselectList('account_type','*','','sort asc');
        return $list;
    }

    public function getAccountTypeAdd($data)
    {
        $list =$this->getAdd('account_type',$data);
        return $list;
    }

    public function getAccountTypeInfo($id)
    {
        $list =$this->getOne('account_type','*',['id'=>$id]);
        return $list;
    }

    public function getAccountTypeEdit($id,$data)
    {
        try{
            $list =$this->getOne('account_type','*',['id'=>$id]);
            if($list['level'] ==1){
                throw new \Exception('系统账目类型无法编辑');
            }else{
                return $this->getEdit('account_type',['id'=>$id],$data);
            }
        } catch (DbException $e) {
            return $e->getMessage();
        }
    }

    public function getAccountTypeDel($id)
    {
        try{
            $list =$this->getOne('account_type','*',['id'=>$id]);
            $arr =$this->getOne('set_account_flow','*',['account_type_id'=>$id]);
            if($list['level'] ==1){
                throw new \Exception('系统账目类型无法删除');
            }else if(count($arr) > 0){
                throw new \Exception('账目类型正在使用,无法删除');
            }else{
                return $this->getDel('account_type',$id);
            }
        } catch (DbException $e) {
            return $e->getMessage();
        }
    }
}